<?php

include _ITSTOCK_INCLUDE_DIR_.'html_search.inc.php';
include _ITSTOCK_INCLUDE_DIR_.'html_message.inc.php';
include _ITSTOCK_INCLUDE_DIR_.'html_navigation.inc.php';
include _ITSTOCK_INCLUDE_DIR_.'html_links.inc.php';
include _ITSTOCK_INCLUDE_DIR_.'html_menu.inc.php';
include _ITSTOCK_INCLUDE_DIR_.'html_table_list.inc.php';

$_SESSION['on_delete']['hardware'] = $_SERVER['REQUEST_URI'];
$_SESSION['on_save']['hardware'] = $_SERVER['REQUEST_URI'];
$_SESSION['on_delete']['cpu'] = $_SERVER['REQUEST_URI'];
$_SESSION['on_save']['cpu'] = $_SERVER['REQUEST_URI'];

$structure = itstock_html_search_init('hardware');
$structure['table_headers'] = array('Type', 'Maker', 'Model', 'Capacity', 'Serial', 'Condition', '', '', '');
$structure['table_indexes'] = array('category', 'maker', 'model', 'capacity', 'serialn', 'state');
$structure['table_actions'] = array(
  'order' =>  null,
  array( 'img' => 'img/edit.png', 'onclick' => 'itstock_page_request(\'add_hardware\', \'%s\')', 'title' => 'Edit', 'id' => 'id' ),
  array( 'link' => 'delete.php?type=hardware&id=%s', 'img' => 'img/delete.png', 'title' => 'Delete', 'id' => 'id')
);
$structure['table_error'] = 'No hardware have been added.';

$location = itstock_html_search_init('location');
if(isset($_GET['id'])) {
  $location['where'][0] = 'locations.id='.itstock_decode($_GET['id']);
  if(itstock_search_all($location)){
    $structure['where'][0] = 'location_id='.$location['data'][0]['id'];
  } else {
    $_SESSION['error'] = 'Please select a valid location.';
    header('Location: main.php?page=location');
    exit();
  }
} else {
  $_SESSION['error'] = 'Please select a location.';
  header('Location: main.php?page=location');
  exit();
}

$itstock_page_info['css'] = array('main.css', 'aside.css', 'float_menu.css', 'page.css', 'table.css', 'lightbox.css', 'form_save.css', 'table_info.css');
$itstock_page_info['js'] = array('common.js', 'ajax.js', 'request.js', 'lightbox.js', 'search.js', 'settings.js');
$itstock_page_info['title'] = 'Location - IT Stock Tool';
$itstock_page_info['menu_title'] = 'Hardware Location';
$itstock_page_info['menu_items'] = array(
  '<label onclick="itstock_page_request(\'add_location\')">New Location</label>',
  '<label onclick="itstock_page_request(\'add_hardware\', undefined,\'loc_id='.$_GET['id'].'\')">Add Hardware</label>',
  '<label onclick="itstock_page_request(\'add_cpu\', undefined,\'loc_id='.$_GET['id'].'\')">Add Case</label>'
);
$itstock_page_info['search_page'] = 'hardware_location';

itstock_html_base_header($itstock_page_info);
itstock_html_menu_search($itstock_page_info);
?>
  <table class="header">
    <tbody>
      <tr><th>Building:</th><td><?php echo itstock_get($location, 'building'); ?></td><th>Spot:</th><td><?php echo itstock_get($location, 'spot'); ?></td></tr>
      <tr><th>Department:</th><td> <?php echo itstock_get($location, 'department'); ?></td><th>Name:</th><td><?php echo itstock_get($location, 'location'); ?></td></tr>
      <tr><th>Floor:</th><td><?php echo itstock_get($location, 'floor'); ?></td></tr>
    </tbody>
  </table>
  <br><br><?php
echo '<table>';
if(itstock_search_all($structure)){
  itstock_html_table_list_navigation($structure);
  ?>
  <tr>
  <td colspan="2">
  <table class="list">
  <?php
  itstock_html_table_list_headers($structure['table_headers']);
  echo '<tbody>';
  foreach($structure['data'] as &$item){
    echo '<tr>';
    foreach($structure['table_indexes'] as $idx) {
      echo '<td>'.$item[$idx].'</td>';
    }
    if(is_null($item['order_item_id'])) {
      $structure['table_actions']['order'] = array( 'img' => 'img/order_disabled.png', 'title' => 'Order Not Found');
    } else {
      $structure['table_actions']['order'] = array( 'link' => 'main.php?page=order_item&id=%s', 'img' => 'img/order.png', 'title' => 'See Order', 'id' => 'order_item_id');
    }
    itstock_html_table_list_links($structure['table_actions'], $item, 'list_center');
    echo '</tr>';
  }
  echo '</tbody>';
} else {
  echo '<tr><td><table><tr><td>'.$structure['table_error'].'</td></tr>';
}
echo '</table></td></tr>';
echo '</table>';
echo '<br><h3>Computer Cases</h3>';
/* Hadrware in Cases */
$cpu = itstock_html_search_init('cpu');
$cpu['where'][0] = 'cpu_location_id='.$location['data'][0]['id'];
if(itstock_search_all($cpu)) {
  $hardware = itstock_html_search_init('hardware');
  $hardware['table_headers'] = array('Type', 'Maker', 'Model', 'Capacity', 'Serial', 'Condition', '', '', '');
  $hardware['table_indexes'] = array('category', 'maker', 'model', 'capacity', 'serialn', 'state');
  $hardware['table_actions'] = array(
    'order' =>  null,
    array( 'img' => 'img/edit.png', 'onclick' => 'itstock_page_request(\'add_hardware\', \'%s\')', 'title' => 'Edit', 'id' => 'id' ),
    array( 'link' => 'delete.php?type=hardware&id=%s', 'img' => 'img/delete.png', 'title' => 'Delete', 'id' => 'id')
  );
  $hardware['table_error'] = 'No hardware have been added.';
  foreach($cpu['data'] as &$case) {
    $hardware['where'][0] = 'cpu_id='.$case['id'];
    $hardware['limit'] = '';
    $case_id = itstock_encode($case['id']);
    echo '<br><label onclick="HS(\'case_'.$case_id.'\')"><b>Case: </b>'.$case['cpu'].'</label><br>';
    echo '<table id="case_'.$case_id.'" style="display:none;">';
    if(itstock_search_all($hardware)){
    ?>
    <tr>
    <td colspan="2">
    <table class="list">
    <?php
      itstock_html_table_list_headers($hardware['table_headers']);
      echo '<tbody>';
      foreach($hardware['data'] as &$item){
      echo '<tr>';
      foreach($hardware['table_indexes'] as $idx) {
        echo '<td>'.$item[$idx].'</td>';
        }
        if(is_null($item['order_item_id'])) {
          $hardware['table_actions']['order'] = array( 'img' => 'img/order_disabled.png', 'title' => 'No Order');
        } else {
          $hardware['table_actions']['order'] = array( 'link' => 'main.php?page=order_item&id=%s', 'img' => 'img/order.png', 'title' => 'See Order', 'id' => 'order_item_id');
        }
        itstock_html_table_list_links($hardware['table_actions'], $item, 'list_center');
        echo '</tr>';
      }
    } else {
      echo '<tr><td><table><tr><td>'.$hardware['table_error'].'</td></tr>';
    }
    echo '</tbody></table></td></tr>';
    echo '</table><br>';
  }
} else {
  echo 'No computer cases have been added.';
}

itstock_html_base_footer();
?>
